﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HyEduOnline.Server.Student;
using HyEduOnline.Server.Common;
using HyEduOnline.Server.Admin;
using HyEduOnline.Entities;
using HyEduOnline.Server.Teacher;
using HyEduOnline.Services;


namespace HyEduOnline.Controllers
{
    [UserAuthorizeStu]
    public class StudentController : Controller
    {
        private StudentsDAO studentsDAO = new StudentsDAO();



        /// <summary>
        /// 学生的导航
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.StuName = frontCurrentStu.GetUserName();
            if (Session["Image"] != null)
            {
                ViewBag.ImageUrl = Session["Image"].ToString();
            }
            return View();
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Home()
        {
            if (frontCurrentStu.TestState() != -1)
                return Redirect("/Student/Null");
            string stuCode = frontCurrentStu.GetNo();//取当前登录的学生学号

            VList<VwDeanStuSelCourse> CourseList = new DeanStuSelCourseDAO().GetByPageVW("S_StuNo='" + stuCode + "'", 0, int.MaxValue);

            ViewBag.Course = CourseList;
            return View();
        }


        public ActionResult Null()
        {
            return View();
        }

        //个人信息
        public ActionResult Personal()
        {
            TbStudents stu = new TbStudents();
            if (frontCurrentStu.TestState() != -1)
                return Redirect("/Student/Null");
            string stuCode = frontCurrentStu.GetNo();
            TList<TbStudents> list = studentsDAO.GetByPage("S_No = '" + stuCode + "'", 0, int.MaxValue);
            if (list.Count > 0)
            {
                stu = list[0];
            }
            return View(stu);
        }

        //修改密码
        public ActionResult ChangePassword()
        {
            if (frontCurrentStu.TestState() != -1)
                return Redirect("/Student/Null");
            return View();
        }

        //课程介绍
        public ActionResult CourseIntro()
        {
            if (frontCurrentStu.TestState() != -1)
                return Redirect("/Student/Null");
            string stuCode = frontCurrentStu.GetNo();//取当前登录的学生学号

            VList<VwDeanStuSelCourse> CourseList = new DeanStuSelCourseDAO().GetByPageVW("S_StuNo='" + stuCode + "'", 0, int.MaxValue);

            ViewBag.CourseList = CourseList;

            return View();
        }


        //课程详情显示页面
        public ActionResult OutlineShow(int CourseId, int TeaID)
        {
            //ViewBag.CourseName = CourseName;//课程名
            ViewBag.TeaID = TeaID;
            ViewBag.CourseID = CourseId;
            ViewBag.Course = new DeanCourseDAO().GetByID(CourseId);
            string imgUrl = "";
            if (!string.IsNullOrEmpty(ViewBag.Course.SImgUrl))
            {
                imgUrl = ViewBag.Course.SImgUrl.Replace("/middle/", "/large/");
            }
            ViewBag.Course.SImgUrl = imgUrl;
            //教学大纲所有内容
            ViewBag.teaCatalog = new TeaCatalogDAO().GetByPage("I_DeanCourseID =" + CourseId, 0, int.MaxValue);
            return View();
        }

        /// <summary>
        /// 获取公开资源
        /// </summary>
        /// <param name="CourseID"></param>
        /// <param name="TeaID"></param>
        /// <returns></returns>
        public ActionResult PublicResource(int CourseID, int TeaID)
        {
            ViewBag.TeaID = TeaID;
            ViewBag.CourseID = CourseID;
            ViewBag.CourseName = new DeanCourseDAO().GetByID(CourseID).SCourseName;
            return View();
        }

        /// <summary>
        /// 学生菜单栏
        /// </summary>
        /// <returns></returns>
        public ActionResult Menu()
        {
            ViewBag.MenuTree = Session["SysMenus"];
            return View();
        }


        public ActionResult PaperList(int CourseID, int TeaID)
        {
            ViewBag.TeaID = TeaID;
            ViewBag.CourseID = CourseID;
            return View();
        }


        //学生答卷
        public ActionResult StuAnswerDetail(int paperId)
        {
            TestStuAnswerDAO testStuAnswerDAO = new TestStuAnswerDAO();
            string stuCode = frontCurrentStu.GetNo();
            //当前答卷信息
            ViewBag.paper = new TestScoresDAO().GetByPage("S_StuCode='" + stuCode + "' and I_PaperID=" + paperId, 0, int.MaxValue)[0];

            ViewBag.CourseId = Session["TempCourseID"];
            List<TestPaperExt> tempStrategyContent = new List<TestPaperExt>();   //定义大题的对象集(策略和其附属的小题)

            string WhereClause = "I_PaperID=" + paperId;  //在策略表中查询试卷的策略
            TList<TbTestPaperStrategy> tempStrategy = new TestPaperStrategyDAO().GetByPage(WhereClause, 0, int.MaxValue);


            if (tempStrategy.Count > 0)   //存在该试卷的策略，同时返回该试卷的策略个数
            {
                foreach (var item in tempStrategy)
                {
                    List<QuestExt> TestLiblist = new List<QuestExt>();   //定义题库的对象集（小题和可能存在的选项）//解决问题所在 9.19
                    TestPaperExt tem = new TestPaperExt();
                    tem.StrategyItem = item;//获取当前的策略
                    tem.stuGetScore = testStuAnswerDAO.GetByPage(
                        "S_StuCode='" + stuCode + "' and I_StrateID=" + item.Id, 0, int.MaxValue)[0].IScore.ToString();
                    string whereClause1 = "I_StrategyID=" + item.Id;
                    TList<TbTestLibPaperDetail> tempDetailList = new TestLibPaperDetailDAO().GetByPage(whereClause1, 0, int.MaxValue); //获取每一个大题中的小题题id
                    if (tempDetailList.Count > 0)
                    {
                        foreach (var item1 in tempDetailList)
                        {
                            int DetailId = (int)item1.Id;
                            QuestExt TtempList = new QuestExt();//获取试题集
                            string libID = item1.ILibId.ToString();
                            int LibID = int.Parse(libID);
                            TbTestLib tempLib = new TestLibDAO().GetByID(LibID); //获取小题的详细信息
                            if (tempLib != null)
                            {

                                TtempList.LibItem = tempLib;   //把每一个试题放到list中
                                var problemtype = tempLib.IProblemType;  //获取策略的大题类型

                                string WhereClause1 = "I_LibID=" + tempLib.Id;
                                TList<TbTestLibOption> optionLists = new TestLibOptionDAO().GetByPage(WhereClause1, 0, int.MaxValue);//获取该题对应的选项
                                if (optionLists.Count > 0)  //该题存在选项
                                {
                                    TtempList.OptionList = optionLists;
                                }

                                //根据题型,筛选学生答案
                                TList<TbTestStuAnswerOptions> stuSelTemp = new TestStuAnswerOptionsDAO().GetByPage(
                                    "S_StuCode='" + stuCode + "' and I_LibID=" + LibID, 0, int.MaxValue);
                                string stuAnswer = "";
                                foreach (var item2 in stuSelTemp)
                                {
                                    string str = new TestLibOptionDAO().GetByID((int)item2.IOptionId).SOptionName;
                                    stuAnswer = stuAnswer + "," + str;
                                }
                                TtempList.StuAnswer = stuAnswer;

                                //该题是否正确
                                TList<TbTestStuAnswerDetail> tempList = new TestStuAnswerDetailDAO().GetByPage(
                                    "S_StuCode='" + stuCode + "' and I_PaperDetailID=" + DetailId, 0, int.MaxValue);
                                if (tempList.Count() > 0)
                                    TtempList.isOk = (int)tempList[0].IIsTrue;

                            }
                            TestLiblist.Add(TtempList);//获取选项集及试题

                        }
                        tem.LibItemList = TestLiblist;//获取当前策略的附属题集 

                    }

                    tempStrategyContent.Add(tem);  //该策略 
                }


            }
            //返回策略的详细信息，返回策略的个数
            ViewBag.TestPaperDetail = tempStrategyContent;

            return View();
        }



        /// <summary>
        /// 公共试题
        /// </summary>
        /// <param name="CourseID"></param>
        /// <returns></returns>
        public ActionResult PublicTestLib(int CourseID)
        {

            ViewBag.CourseID = CourseID;
            string whereClause = "I_ParentID=0  and I_DeanCourseID=" + CourseID;
            ViewBag.Charpter = new TeaCatalogDAO().GetByPage(whereClause, 0, int.MaxValue);

            return View();
        }

        /// <summary>
        /// 学生习题
        /// </summary>
        /// <param name="courseId"></param>
        /// <returns></returns>
        public ActionResult Practice(int courseId)
        {
            ViewBag.CourseID = courseId;

            ViewBag.no = frontCurrentStu.GetNo();
            return View();
        }


        //学生练习
        public ActionResult PracticeView(int praId)
        {
            List<int> libList = new List<int>();//题目数量列表
            TestLibDAO libDAO = new TestLibDAO();
            TestLibOptionDAO optionDAO = new TestLibOptionDAO();
            int[] tempStrategy = { 1, 2, 3 };
            string stuCode = frontCurrentStu.GetNo();
            //当前习题信息
            TbStuPracticeList practice = new StuPracticeListDAO().GetByID(praId);
            ViewBag.pra = practice;
            int courseId = (int)practice.ICourseId;
            if (practice == null)
                return Json(0);

            string[] catalog = practice.SCatalog.Split(new char[] { ',' });

            List<TestPaperExt> tempStrategyContent = new List<TestPaperExt>();   //定义大题的对象集(策略和其附属的小题)

            if (tempStrategy.Length > 0)   //存在该试卷的策略，同时返回该试卷的策略个数
            {
                foreach (var item in tempStrategy)
                {
                    List<QuestExt> TestLiblist = new List<QuestExt>();   //定义题库的对象集（小题和可能存在的选项）//解决问题所在 9.19
                    TestPaperExt tem = new TestPaperExt();
                    int libCount = 0;//小题数量
                    TbTestPaperStrategy StrategyItem = new TbTestPaperStrategy();
                    StrategyItem.Id = item;
                    tem.StrategyItem = StrategyItem;//获取当前的策略


                    TList<TbTestLib> tempDetailList = new TList<TbTestLib>();
                    foreach (var cata in catalog)
                    {
                        TList<TbTestLib> tempLibLi = libDAO.GetByPage(
                            "I_CousreID=" + courseId
                            + " and I_StuOpen = 1 and I_ProblemType=" + item
                            + " and I_TeaCatalog = " + cata, 0, int.MaxValue);
                        tempDetailList.AddRange(tempLibLi);
                    }

                    foreach (var item1 in tempDetailList)
                    {

                        QuestExt TtempList = new QuestExt();//获取试题集
                        
                        TbTestLib tempLib = item1; //获取小题的详细信息
                        if (tempLib != null)
                        {
                            TtempList.LibItem = tempLib;   //把每一个试题放到list中
                            var problemtype = tempLib.IProblemType;  //获取策略的大题类型

                            string WhereClause1 = "I_LibID=" + tempLib.Id;
                            TList<TbTestLibOption> optionLists = new TestLibOptionDAO().GetByPage(WhereClause1, 0, int.MaxValue);//获取该题对应的选项
                            if (optionLists.Count > 0)  //该题存在选项
                            {
                                TtempList.OptionList = optionLists;
                            }
                            libCount++;
                        }
                        TestLiblist.Add(TtempList);//获取选项集及试题
                        
                    }
                    tem.LibItemList = TestLiblist;//获取当前策略的附属题集 


                    libList.Add(libCount);
                    tempStrategyContent.Add(tem);  //该策略 
                }


            }
            //计算每到大题总分
            List<int> score = new List<int>();
            int scTmp = (int)(100 / (libList[0] + libList[1] + libList[2]));
            foreach (var item in libList)
            {
                score.Add(item * scTmp);
            }
            ViewBag.oneScore = scTmp;
            ViewBag.score = score;
            ViewBag.TestPaperDetail = tempStrategyContent;

            return View();
        }


        //学生查看练习集页面
        public ActionResult PracticeResult(int praId,string stuCode = "")
        {

            int oneLib = 0; //小题分数
            TestLibDAO libDAO = new TestLibDAO();
            TestLibOptionDAO optionDAO = new TestLibOptionDAO();
            int[] tempStrategy = { 1, 2, 3 };
            List<int> score = new List<int>();
            //当前习题信息
            if (string.IsNullOrEmpty(stuCode))
            {
                stuCode = frontCurrentStu.GetNo();
            }
            TbStuPractice practice = new StuPracticeDAO().GetByPage(
                "I_PracticeID=" + praId + " and S_StuCode='" + stuCode + "'", 0, int.MaxValue)[0];
            ViewBag.pra = practice;
            int courseId = (int)practice.ICourseId;
            if (practice == null)
                return Json(0);
            //取出当前学生的信息
            ViewBag.stu = new StudentsDAO().findBySNo(stuCode)[0];

            //取出练习的章节
            TbStuPracticeList pra = new StuPracticeListDAO().GetByID(praId);
            string[] catalog = pra.SCatalog.Split(new char[] { ',' });
            //取出练习的章节 END

            List<TestPaperExt> tempStrategyContent = new List<TestPaperExt>();   //定义大题的对象集(策略和其附属的小题)

            if (tempStrategy.Length > 0)   //存在该试卷的策略，同时返回该试卷的策略个数
            {
                foreach (var item in tempStrategy)
                {
                    int stuScore = 0;//学生得分
                    int libCount = 0;//小题数量

                    List<QuestExt> TestLiblist = new List<QuestExt>();   //定义题库的对象集（小题和可能存在的选项）//解决问题所在 9.19
                    TestPaperExt tem = new TestPaperExt();
                    TbTestPaperStrategy StrategyItem = new TbTestPaperStrategy();
                    StrategyItem.Id = item;
                    tem.StrategyItem = StrategyItem;//获取当前的策略
                    TList<TbTestLib> tempDetailList = new TList<TbTestLib>();
                    foreach (var cata in catalog)
                    {
                        TList<TbTestLib> tempLibLi = libDAO.GetByPage(
                            "I_CousreID=" + courseId
                            + " and I_StuOpen = 1 and I_ProblemType=" + item
                            + " and I_TeaCatalog = " + cata, 0, int.MaxValue);
                        tempDetailList.AddRange(tempLibLi);
                    }


                    foreach (var item1 in tempDetailList)
                    {

                        QuestExt TtempList = new QuestExt();//获取试题集

                        TbTestLib tempLib = item1; //获取小题的详细信息
                        if (tempLib != null)
                        {
                            TtempList.LibItem = tempLib;   //把每一个试题放到list中
                            var problemtype = tempLib.IProblemType;  //获取策略的大题类型

                            string WhereClause1 = "I_LibID=" + tempLib.Id;
                            TList<TbTestLibOption> optionLists = new TestLibOptionDAO().GetByPage(WhereClause1, 0, int.MaxValue);//获取该题对应的选项
                            if (optionLists.Count > 0)  //该题存在选项
                            {
                                TtempList.OptionList = optionLists;
                            }

                            //根据题型,筛选学生答案
                            TList<TbStuPracticeOptions> stuSelTemp = new StuPracticeOptionsDAO().GetByPage(
                                "S_StuCode='" + stuCode + "' and I_LibID=" + tempLib.Id + " and I_PracticeID=" + praId, 0, int.MaxValue);
                            string stuAnswer = "";
                            foreach (var item2 in stuSelTemp)
                            {
                                string str = new TestLibOptionDAO().GetByID((int)item2.IOptionId).SOptionName;
                                stuAnswer = stuAnswer + "," + str;
                            }

                            TtempList.StuAnswer = stuAnswer;

                            //该题是否正确
                            TList<TbStuPracticeDetail> tempList = new StuPracticeDetailDAO().GetByPage(
                                "S_StuCode='" + stuCode + "' and I_PraID=" + praId
                                + " and I_LibID=" + tempLib.Id, 0, int.MaxValue);

                            if (tempList.Count() > 0)
                            {
                                int IScore = (int)tempList[0].IScore;
                                TtempList.isOk = (int)tempList[0].IIsTrue;
                                stuScore += IScore;
                                if (oneLib == 0)
                                {
                                    if (IScore != 0)
                                    {
                                        oneLib = (int)tempList[0].IScore;
                                    }
                                }
                            }
                            libCount++;
                        }

                        TestLiblist.Add(TtempList);//获取选项集及试题

                    }
                    score.Add(libCount * oneLib);
                    tem.stuGetScore = stuScore.ToString();
                    tem.LibItemList = TestLiblist;//获取当前策略的附属题集 



                    tempStrategyContent.Add(tem);  //该策略 
                }


            }

            ViewBag.score = score;
            ViewBag.TestPaperDetail = tempStrategyContent;

            return View();
        }
    }
}
